package com.kaili.tms.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.common.exception.BaseExcept;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.tms.exception.ResponseStatus;
import com.kaili.tms.pojo.dto.ExpenseAccountAddDTO;
import com.kaili.tms.pojo.dto.ExpenseAccountQueryDTO;
import com.kaili.tms.pojo.entity.ExpenseAccount;
import com.kaili.tms.pojo.vo.ExpenseAccountVO;
import com.kaili.tms.service.ExpenseAccountService;
import com.kaili.tms.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;


@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Validated
@Api(tags = "报销管理", description = "报销管理相关API")
public class ExpenseAccountController {

    @Autowired
    private ExpenseAccountService expenseAccountService;

    @ApiOperation(value = "新增发票报销信息")
    @PostMapping("/expenseAccount")
    public RestStatus addExpenseAccount(HttpServletRequest request){
        ExpenseAccount expenseAccount = getFormData(request);
        if (expenseAccountService.selectExpenseAccount(expenseAccount) != null) {
            throw new BaseExcept(ResponseStatus.EXPENSE_REPEAT);
        }
        expenseAccountService.addExpenseAccount(expenseAccount);
        return CommonStatus.ADD_OK;
    }

    @ApiOperation(value = "更新发票报销信息")
    @PostMapping("/expenseAccount/data")
    public RestStatus updateExpenseAccount(HttpServletRequest request){
        Integer invoiceId = Integer.valueOf(request.getParameter("invoiceId"));
        ExpenseAccount expenseAccount = getFormData(request);
        expenseAccount.setInvoiceId(invoiceId);
        expenseAccountService.updateExpenseAccount(expenseAccount);
        return CommonStatus.UPDATE_OK;
    }

    @ApiOperation(value = "批量更新报销状态")
    @PutMapping("/expenseAccount/bat")
    public RestStatus updateExpenseAccountBat(@RequestBody ExpenseAccountAddDTO addDTO){
        ExpenseAccount expenseAccount = new ExpenseAccount();
        List<Integer> invoiceIds = addDTO.getInvoiceId();
        for (Integer invoiceId : invoiceIds) {
            expenseAccount.setInvoiceId(invoiceId);
            expenseAccount.setStatus(addDTO.getStatus());
            expenseAccount.setPayment(addDTO.getPayment());
            ExpenseAccount ea = expenseAccountService.selectExpenseAccountBy(invoiceId);
            if (expenseAccount.getStatus() == 1 && ea.getStatus() != 0) {
                throw new BaseExcept(ResponseStatus.EXPENSE_HANDLED);
            }
            if (expenseAccount.getStatus() == 2 && ea.getStatus() != 1) {
                throw new BaseExcept(ResponseStatus.EXPENSE_ALREADY);
            }
            expenseAccountService.updateExpenseAccount(expenseAccount);
        }
        return CommonStatus.UPDATE_OK;
    }

    @GetMapping("/expenseAccounts")
    @ApiOperation(value = "分页查询报销信息")
    public Page<ExpenseAccountVO> getBaseDriverByKeywords(@ModelAttribute ExpenseAccountQueryDTO queryDTO){
        Page<ExpenseAccountVO> pager = new Page<>(queryDTO.getPage(),queryDTO.getPageSize());
        return expenseAccountService.queryExpenseAccount(pager, queryDTO);
    }


    private ExpenseAccount getFormData(HttpServletRequest request) {
        String invoiceCode = request.getParameter("invoiceCode");
        Integer invoiceType = Integer.valueOf(request.getParameter("invoiceType"));
        double oil = Double.parseDouble(request.getParameter("oil"));
        double account = Double.parseDouble(request.getParameter("account"));
        Integer applyBy = Integer.valueOf(request.getParameter("applyBy"));
        Integer applyType = Integer.valueOf(request.getParameter("applyType"));
        Integer carCode = Integer.valueOf(request.getParameter("carCode"));
        String createDate = request.getParameter("createDate");
        String lineNum = request.getParameter("lineNum");
        String trainNum = request.getParameter("trainNum");
        String provenance = request.getParameter("provenance");
        String destination = request.getParameter("destination");
        String url = request.getParameter("url");
        String payment = request.getParameter("payment");

        String remark = request.getParameter("remark");

        ExpenseAccount expenseAccount = new ExpenseAccount();
        expenseAccount.setInvoiceCode(invoiceCode);
        expenseAccount.setInvoiceType(invoiceType);
        expenseAccount.setOil(oil);
        expenseAccount.setAccount(account);
        expenseAccount.setApplyBy(applyBy);
        expenseAccount.setApplyType(applyType);
        expenseAccount.setCarCode(carCode);
        expenseAccount.setCreateDate(createDate);
        expenseAccount.setLineNum(lineNum);
        expenseAccount.setTrainNum(trainNum);
        if (provenance != null && !provenance.trim().equals("")) {
            expenseAccount.setProvenance(Integer.valueOf(provenance));
        }
        if (provenance != null && !provenance.trim().equals("")) {
            expenseAccount.setDestination(Integer.valueOf(destination));
        }
        if (payment != null && !payment.trim().equals("")) {
            expenseAccount.setPayment(Integer.valueOf(payment));
        }
        expenseAccount.setSite(LoginDetailUtils.getSiteId());
        expenseAccount.setRemark(remark);

        MultipartFile file = ((MultipartHttpServletRequest) request).getFile("file");
        try {
            if (file != null) {
                String path = UploadController.saveFile(file);
                expenseAccount.setUrl(path);
            }else {
                if (url.equals("")){
                    expenseAccount.setUrl(url);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return expenseAccount;
    }
}
